package com.yu.subject.domain.handler.subject;

import com.yu.subject.common.enums.SubjectInfoTypeEnum;
import com.yu.subject.common.util.BeanCopyUtil;
import com.yu.subject.domain.entity.SubjectAnswerBO;
import com.yu.subject.domain.entity.SubjectInfoBO;
import com.yu.subject.domain.entity.SubjectOptionBO;
import com.yu.subject.infra.basic.entity.SubjectRadio;
import com.yu.subject.infra.basic.service.SubjectRadioService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 路亚
 * @version 0.1
 * @className RadioTypeHandler
 * @description
 * @date 2024/12/24 22:32
 * @since jdk11
 */
@Component
public class RadioTypeHandler implements SubjectTypeHandler{

    @Resource
    private SubjectRadioService subjectRadioService;

    @Override
    public SubjectInfoTypeEnum getHandlerType() {
        return SubjectInfoTypeEnum.RADIO;
    }

    @Override
    public void add(SubjectInfoBO subjectInfoBO) {
        // 插入
        List<SubjectAnswerBO> optionList = subjectInfoBO.getOptionList();
        List<SubjectRadio> list = optionList.stream().map(option -> {
            SubjectRadio subjectRadio = new SubjectRadio();
            BeanUtils.copyProperties(option, subjectRadio);
            subjectRadio.setSubjectId(subjectInfoBO.getId());
            return subjectRadio;
        }).collect(Collectors.toList());
        subjectRadioService.saveBatch(list);
    }

    @Override
    public SubjectOptionBO query(Integer subjectId) {
        return null;
    }
}
